package com.ymatou.iapi.im.testcase;

import com.google.gson.JsonObject;
import com.ymatou.iapi.im.parameter.SendMessageBean;
import com.ymatou.iapi.im.service.SendMessageCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.ImCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.YmtMessage;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import org.json.JSONObject;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
*客服发送消息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendMessage{
	private static SendMessageBean sendmessageBean;
	private static SendMessageCall sendmessageCall;
	private static int imId = Integer.parseInt(EnvSetup.getData("userId_IM"));
	private static String imName = EnvSetup.getData("userName_IM");
	private static ImDefaultWapper im = new ImDefaultWapper();
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("客服发送消息，洋管家");
	}

	@Before
	public void caseUp() {
		sendmessageBean=new SendMessageBean();
		sendmessageCall=new SendMessageCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendmessageCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_001() {
		Logger.start(true,"客服向买家发送消息");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			sendmessageBean = ImCallService.initSendMessage(imId,userId,imName);
			sendmessageCall.setData(sendmessageBean);
			sendmessageCall.callService();
			Logger.verifyEquals("Success",sendmessageCall.getString("Status"),"验证Status");
			verifyDB(sendmessageBean);
			//计数服务
			int unRead = ImCallService.GetMessageCount(sendmessageBean.getToUserId());
			Logger.verifyEquals(1,unRead,"验证买家有一条消息未读");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_007() {
		Logger.start(true,"客服向买手发送消息");
		try {
			sendmessageBean = ImCallService.initSendMessage(imId,sellerId,imName);
			sendmessageCall.setData(sendmessageBean);
			sendmessageCall.callService();
			Logger.verifyEquals("Success",sendmessageCall.getString("Status"),"验证Status");
			verifyDB(sendmessageBean);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_004() {
		Logger.start(true,"客服向买家发送图片，发送成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			sendmessageBean = ImCallService.initSendMessage(imId,userId,imName);
			String message = "[img]http://pic1.ymatou.com/G01/M00/2F/5D/rBBlD1hg8JqAcw_pAAOQn3fB5wk437_8_5_o.jpg#274x171[/img]";
			sendmessageBean.setMessage(message);
			sendmessageCall.setData(sendmessageBean);
			sendmessageCall.callService();
			Logger.verifyEquals("Success",sendmessageCall.getString("Status"),"验证Status");
			verifyDB(sendmessageBean);
			int unRead = ImCallService.GetMessageCount(sendmessageBean.getToUserId());
			Logger.verifyEquals(1,unRead,"验证买家有一条消息未读");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_005() {
		Logger.start(true,"客服向买家发送表情，发送成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			sendmessageBean = ImCallService.initSendMessage(imId,userId,imName);
			String message = "😁";
			sendmessageBean.setMessage(message);
			sendmessageCall.setData(sendmessageBean);
			sendmessageCall.callService();
			Logger.verifyEquals("Success",sendmessageCall.getString("Status"),"验证Status");
			verifyDB(sendmessageBean);
			int unRead = ImCallService.GetMessageCount(sendmessageBean.getToUserId());
			Logger.verifyEquals(1,unRead,"验证买家有一条消息未读");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_006() {
		Logger.start(true,"客服向买家发送特殊字符，发送成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			sendmessageBean = ImCallService.initSendMessage(imId,userId,imName);
			String message = "！）（*&……@￥测试测试导出";
			sendmessageBean.setMessage(message);
			sendmessageCall.setData(sendmessageBean);
			sendmessageCall.callService();
			Logger.verifyEquals("Success",sendmessageCall.getString("Status"),"验证Status");
			verifyDB(sendmessageBean);
			int unRead = ImCallService.GetMessageCount(sendmessageBean.getToUserId());
			Logger.verifyEquals(1,unRead,"验证买家有一条消息未读");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_002() {
		Logger.start(true,"客服发送消息,消息为空");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			sendmessageBean.setFromUserId(imId);
			sendmessageBean.setToUserId(userId);
			sendmessageBean.setCSId(imId);
			sendmessageBean.setCSName(imName);
			sendmessageBean.setSessionNumber(imId+"_"+userId);
			sendmessageBean.setAutoSetRead(true);
			sendmessageBean.setIsRiskyUser(false);
			sendmessageCall.setData(sendmessageBean);
			sendmessageCall.callService();
			Logger.verifyEquals("ParameterError", new JSONObject(sendmessageCall.getReturnData()).getString("Status"), "响应报文-返回码");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_003() {
		Logger.start(true,"客服发送消息，userid为空");
		try {
			String message = "nihao！hello world";
			sendmessageBean.setMessage(message);
			sendmessageBean.setFromUserId(imId);
			sendmessageBean.setCSId(imId);
			sendmessageBean.setCSName(imName);
			sendmessageBean.setSessionNumber(imId+"_");
			sendmessageBean.setAutoSetRead(true);
			sendmessageBean.setIsRiskyUser(false);
			sendmessageCall.setData(sendmessageBean);
			sendmessageCall.callService();
			Logger.verifyEquals("BussinessError", new JSONObject(sendmessageCall.getReturnData()).getString("Status"), "响应报文-返回码");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}


	public void verifyDB(SendMessageBean sendMessageBean) throws InterruptedException, IOException, ParseException {
		Map m=(Map) sendmessageCall.getMap().get("Result");
		String[] msgids=m.get("messageId").toString().split("_");
		String msgid=msgids[msgids.length-1];
		//验证ymt_immessage.ymt_message_61表中是否增加一条消息记录
		YmtMessage map=im.selectImInfo(sendMessageBean.getToUserId(), sendMessageBean.getFromUserId(), Integer.parseInt(msgid)).get(0);
		//验证ymt_session表中是否有记录
		Map mapsession1 = im.selectTotalCount(sendMessageBean.getFromUserId(),sendMessageBean.getToUserId()).get(0);
		Map mapsession2 = im.selectTotalCount(sendMessageBean.getToUserId(),sendMessageBean.getFromUserId()).get(0);
		Logger.verifyEquals(sendMessageBean.getFromUserId(),mapsession1.get("OtherUserId"),"验证imId");
		Logger.verifyEquals(sendMessageBean.getFromUserId(),mapsession2.get("UserId"),"验证imId");
		Logger.verifyEquals(sendMessageBean.getToUserId(),mapsession1.get("UserId"),"验证userId");
		Logger.verifyEquals(sendMessageBean.getToUserId(),mapsession2.get("OtherUserId"),"验证userId");
		int MsgFromId = map.getMsgFromId();
		int MsgToId= map.getMsgToId();
		String MessageContent=map.getMessage();
		JsonObject ret = sendmessageCall.getJsonObject().get("Result").getAsJsonObject();
		Logger.verifyEquals(sendMessageBean.getToUserId(), MsgToId, "数据库验证ToUserId");
		Logger.verifyEquals(sendMessageBean.getFromUserId(), MsgFromId, "数据库验证FromUserId");
		Logger.verifyEquals(sendMessageBean.getMessage(), MessageContent, "数据库验证Message");
		Logger.verifyEquals(ret.get("toUserName").getAsString(),JUserCallService.GetUserInfo(sendMessageBean.getToUserId()).getLoginId(),"验证买家名称");
		Logger.verifyEquals(ret.get("fromUserLogo").getAsString(),JUserCallService.GetLogoUrl(sendMessageBean.getFromUserId()),"验证买手logo");
		Logger.verifyEquals(ret.get("toUserLogo").getAsString(),JUserCallService.GetLogoUrl(sendMessageBean.getToUserId()),"验证买家logo");
		String tgPostTime = ret.get("postTime").getAsString();
		DateFormat format = new SimpleDateFormat(YMTDateUtil.YMDTHMS_STRING);
		Date time = format.parse(tgPostTime);
		AssertService.assertTime(time,map.getPostTime(),2,"验证PostTime");
		Logger.verifyEquals(ret.get("isCancel").getAsBoolean(),map.getIsCancel()==0?false:true,"验证isCancel");
	}

}